{
  LX Image module

  Based on modo's SDK lximage.h which is:

  Copyright (c) 2008-2012 Luxology LLC

  Permission is hereby granted, free of charge, to any person obtaining a
  copy of this software and associated documentation files (the "Software"),
  to deal in the Software without restriction, including without limitation
  the rights to use, copy, modify, merge, publish, distribute, sublicense,
  and/or sell copies of the Software, and to permit persons to whom the
  Software is furnished to do so, subject to the following conditions:

  The above copyright notice and this permission notice shall be included in
  all copies or substantial portions of the Software.   Except as contained
  in this notice, the name(s) of the above copyright holders shall not be
  used in advertising or otherwise to promote the sale, use or other dealings
  in this Software without prior written authorization.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  DEALINGS IN THE SOFTWARE.
}

{
  Copyright (c) 2012 David Ballesteros and Jameel Halabi

  This unit is part of the BLAISEmodo wrapper,
  which is licensed under the BSD 2-Clause license (see licenses/license.txt).
}


unit Lx_Image;

{$mode objfpc}{$H+}

interface

uses
  Lx_Result, Lx_Value, Lx_VMath;

const
  LXa_IMAGE               = 'image'           ;
  LXa_IMAGESEGMENT        = 'imagesegment'    ;
  LXa_IMAGEWRITE          = 'imagewrite'      ;
  LXa_LAYEREDIMAGE        = 'layeredimage'    ;
  LXa_IMAGELEVELSAMPLE    = 'imageLevelSample';
  LXa_IMAGEBLOCKCODEC     = 'imageBlockCodec' ;
  LXa_MOVIE               = 'movie'           ;

  LXiIMD_BYTE             = $00 ;
  LXiIMD_FLOAT            = $08 ;
  LXfIMD_MASK             = $08 ;
  LXiIMV_GREY             = $01 ;
  LXiIMV_RGB              = $03 ;
  LXiIMV_RGBA             = $04 ;
  LXfIMV_MASK             = $07 ;
  LXiIMT_INDEX            = $100;
  LXiIMT_MASK             = $100;

  LXiIMP_GREY8            =  LXiIMD_BYTE  or LXiIMV_GREY;
  LXiIMP_GREYFP           =  LXiIMD_FLOAT or LXiIMV_GREY;
  LXiIMP_RGB24            =  LXiIMD_BYTE  or LXiIMV_RGB ;
  LXiIMP_RGBFP            =  LXiIMD_FLOAT or LXiIMV_RGB ;
  LXiIMP_RGBA32           =  LXiIMD_BYTE  or LXiIMV_RGBA;
  LXiIMP_RGBAFP           =  LXiIMD_FLOAT or LXiIMV_RGBA;

  LXiIMP_IGREY8           = (LXiIMD_BYTE  or LXiIMV_GREY) or LXiIMT_INDEX;
  LXiIMP_IRGB24           = (LXiIMD_BYTE  or LXiIMV_RGB ) or LXiIMT_INDEX;
  LXiIMP_IRGBA32          = (LXiIMD_BYTE  or LXiIMV_RGBA) or LXiIMT_INDEX;

  LXiPROCESS_FAST         = 0;
  LXiPROCESS_MEDIUM       = 1;
  LXiPROCESS_ACCURATE     = 2;
  LXiIMC_RED              = 0;
  LXiIMC_GREEN            = 1;
  LXiIMC_BLUE             = 2;
  LXiIMC_ALPHA            = 3;

  LXi_IMAGE_GENERATOR     = 0;
  LXi_IMAGE_SINGLE_SAMPLE = 1;
  LXi_IMAGE_MULTI_SAMPLE  = 2;

  LXsIATTRNAME_DPI        = 'DPI'        ;
  LXsIATTRNAME_PIXASPECT  = 'pixelAspect';
  LXsIATTRNAME_REGX0      = 'regionX0'   ;
  LXsIATTRNAME_REGX1      = 'regionX1'   ;
  LXsIATTRNAME_REGY0      = 'regionY0'   ;
  LXsIATTRNAME_REGY1      = 'regionY1'   ;
  LXsIATTRNAME_GAMMA      = 'gamma'      ;
  LXsIATTRNAME_COMMENT    = 'comment'    ;
  LXsIATTRNAME_LUMA       = 'luminance'  ;
  LXsIATTRNAME_FOCDIST    = 'focusDist'  ;
  LXsIATTRNAME_APERTURE   = 'aperture'   ;

  LXsIATTRTYPE_DPI        = LXsTYPE_FLOAT  ;
  LXsIATTRTYPE_PIXASPECT  = LXsTYPE_FLOAT  ;
  LXsIATTRTYPE_REGX0      = LXsTYPE_INTEGER;
  LXsIATTRTYPE_REGX1      = LXsTYPE_INTEGER;
  LXsIATTRTYPE_REGY0      = LXsTYPE_INTEGER;
  LXsIATTRTYPE_REGY1      = LXsTYPE_INTEGER;
  LXsIATTRTYPE_GAMMA      = LXsTYPE_FLOAT  ;
  LXsIATTRTYPE_COMMENT    = LXsTYPE_STRING ;
  LXsIATTRTYPE_LUMA       = LXsTYPE_FLOAT  ;
  LXsIATTRTYPE_FOCDIST    = LXsTYPE_FLOAT  ;
  LXsIATTRTYPE_APERTURE   = LXsTYPE_FLOAT  ;

  LXsMOVIE_SAVESTEREO     = 'movie.stereoscopic';

  LXu_IMAGE               = '{469AFBB8-E6A2-4d88-9C39-5430CA72E404}';
  LXu_IMAGESEGMENT 	  = '{370ABB2F-119E-4EEC-98F8-03388ABE7D2C}';
  LXu_IMAGEWRITE          = '{8F9CF293-B6F9-498e-A124-6704E2C24662}';
  LXu_INDEXIMAGE          = '{08CAE3BC-ED69-4EC4-9D7D-BBDBE4E35343}';
  LXu_INDEXIMAGEWRITE     = '{D59C5E09-779E-445b-B317-282630CB6BD5}';
  LXu_LAYEREDIMAGE        = '{8523ECC4-B8B3-4C6E-8F14-A1D2D01E8038}';
  LXu_LAYEREDIMAGEWRITE   = '{79D28886-9652-4A76-9AA7-1B1E4D553DCD}';
  LXu_IMAGELEVELSAMPLE    = '{79A6EA0E-9589-4D03-880C-C06A6EC6CF7D}';
  LXu_IMAGESERVICE        = '{03A7D258-397C-4D92-B3AF-695AD676FCF9}';
  LXu_IMAGEBLOCKCODEC     = '{7960B5BA-70D3-4FAD-8CA1-BD30A7938554}';
  LXu_IMAGEFILTER         = '{FB00C87C-5A34-4B45-B0E8-998926C79DC4}';
  LXu_IMAGEFILTERSAMPLE   = '{9EC74AED-F86F-4105-ACE9-7BDAA6298736}';
  LXu_IMAGEFILTERMETRICS  = '{0D2CA8D8-66A9-45A0-AAE6-8364D903AFDA}';
  LXu_MOVIE               = '{B7BD9F49-9400-45F1-ADEE-BFE82A1C4A65}';

type
  PLXtPixelFormat = ^LXtPixelFormat;
  PLXtProcessHint = ^LXtProcessHint;
  PLXtImageByte   = ^LXtImageByte  ;
  PLXtImageFloat  = ^LXtImageFloat ;
  PLXtImageIndex  = ^LXtImageIndex ;
  PLXtColorRGBA   = ^LXtColorRGBA  ;

  LXtPixelFormat  = DWord                        ;
  LXtProcessHint  = DWord                        ;
  LXtImageByte    = Byte                         ;
  LXtImageFloat   = Single                       ;
  LXtImageIndex   = DWord                        ;
  LXtColorRGBA    = array [0..3] of LXtImageFloat;

  PLXtImageTarget = ^LXtImageTarget;
  LXtImageTarget  = record
    &Type        : LXtPixelFormat;
    W, H, NColor : UInt32        ;
  end;

  PLXtImageSample = ^LXtImageSample;
  LXtImageSample  = record
    X, Y : UInt32        ;
    RGBA : PLXtImageFloat;
  end;

  PLXtImageMetrics = ^LXtImageMetrics;
  LXtImageMetrics  = record
    MaxRes    : array [0..1  ] of Integer;
    PixelType : Integer                  ;
    Aspect    : Double                   ;
    Filename  : array [0..511] of Char   ;
    Format    : array [0..511] of Char   ;
  end;

  ILxImage = interface
  [LXu_IMAGE]
    procedure Size    (W, H  : PUInt32       ) ;
    function  Format                           : LXtPixelFormat;
    function  GetPixel(X, Y  : UInt32        ;
                       &Type : LXtPixelFormat;
                       Pixel : Pointer       ) : LxResult      ;
    function  GetLine (Y     : UInt32        ;
                       &Type : LXtPixelFormat;
                       Buf   : Pointer       ) : Pointer       ;
  end;

  ILxImageSegment = interface
  [LXu_IMAGESEGMENT]
    function GetSegment(      Y, Left, Right : UInt32        ;
                              RGBA           : PLXtImageFloat) : LxResult;
    function SetSegment(      Y, Left, Right : UInt32        ;
                              &Type          : LXtPixelFormat;
                        const Line           : Pointer       ) : LxResult;
  end;

  ILxImageWrite = interface
  [LXu_IMAGEWRITE]
    procedure Size        (      W, H        : PUInt32       ) ;
    function  Format                                           : LXtPixelFormat;
    function  AddAttribute(const Name, &Type : PChar         ;
                                 Index       : PUInt32       ) : LxResult      ;
    function  SetPixel    (      X, Y        : UInt32        ;
                                 Pxtype      : LXtPixelFormat;
                                 Pixel       : Pointer       ) : LxResult      ;
    function  SetLine     (      Y           : UInt32        ;
                                 PxType      : LXtPixelFormat;
                                 Line        : Pointer       ) : LxResult      ;
  end;

  ILxIndexImage = interface
  [LXu_INDEXIMAGE]
    procedure Size    (W, H      : PUInt32       ) ;
    function  Format                               : LXtPixelFormat;
    function  MapSize (NumColors : PUInt32       ) : LXtPixelFormat;
    function  GetIndex(X, Y      : UInt32        ;
                       Index     : PLXtImageIndex) : LxResult      ;
    function  Getmap  (Index     : LXtImageIndex ;
                       &Type     : LXtPixelFormat;
                       Pixel     : Pointer       ) : LxResult      ;
  end;

  ILxIndexImageWrite = interface
  [LXu_INDEXIMAGEWRITE]
    procedure Size        (      W, H        : PUInt32       ) ;
    function  Format                                           : LXtPixelFormat;
    function  AddAttribute(const Name, &Type : PChar         ;
                                 Index       : UInt32        ) : LxResult      ;
    function  SetIndex    (      X, Y        : UInt32        ;
                                 Index       : LXtImageIndex ) : LxResult      ;
    function  SetMap      (      Index       : LXtImageIndex ;
                                 &Type       : LXtPixelFormat;
                           const Pixel       : Pointer       ) : LxResult      ;
  end;

  ILxLayeredImage = interface
  [LXu_LAYEREDIMAGE]
    procedure Size       (      W, H         : PUInt32 ) ;
    function  Count                                      : UInt32  ;
    function  Image      (      Index        : UInt32  ;
                            out VObj                   ) : LxResult;
    function  Name       (      Index        : UInt32  ;
                          const Name         : PPChar  ) : LxResult;
    function  &Type      (      Index        : UInt32  ;
                                Flags        : PUInt32 ;
                          const &Type        : PPChar  ) : LxResult;
    function  Offset     (      Index        : UInt32  ;
                                X, Y         : PInteger) : LxResult;
    function  Blend      (      Index        : UInt32  ;
                                Flags        : PSingle ;
                          const Mode         : PChar   ) : LxResult;
    function  ChannelName(      LayerIndex   : UInt32  ;
                                ChannelIndex : UInt32  ;
                          const Name_        : PPChar  ) : LxResult;
  end;

  ILxLayeredImageWrite = interface
  [LXu_LAYEREDIMAGEWRITE]
    function Addlayer     (      Image        : IUnknown ;
                           const Name         : PChar    ;
                           const ChannelNames : PPChar   ) : LxResult;
    function SetType      (      Index        : UInt32   ;
                                 Flags        : Integer  ;
                           const &Type        : PChar    ) : LxResult;
    function SetOffset    (      Index        : UInt32   ;
                                 X, Y         : Integer  ) : LxResult;
    function SetBlending  (      Index        : UInt32   ;
                                 Blend        : Single   ;
                           const Mode         : PChar    ) : LxResult;
    function AddAttribute (const Name, &Type  : PChar    ;
                                 Index        : PInteger ) : LxResult;
  end;

  ILxImageLevelSample = interface
  [LXu_IMAGELEVELSAMPLE]
    function Count                                          : UInt32        ;
    function GetLevelSize( Level         : UInt32         ;
                           Width, Height : PUInt32        ) : LxResult      ;
    function SampleLevel ( Level         : UInt32         ;
                           Pixel         : PLXtImageSample;
                           Num           : UInt32         ) : LxResult      ;
    function GetPixel    ( Level, X, Y   : UInt32         ;
                           &Type         : LXtPixelFormat ;
                           Pixel         : Pointer        ) : LxResult      ;
    function GetLine     ( Level, Y      : UInt32         ;
                           Buf           : PLXtImageFloat ) : PLXtImageFloat;
  end;

  ILxImageService = interface
  [LXu_IMAGESERVICE]
    function ScriptQuery(  out VObj                             ) : LxResult;
    function Create     (      Width, Height    : UInt32        ;
                               &Type            : LXtPixelFormat;
                               MaxIndex         : UInt32        ;
                           out VObj                             ) : LxResult;
    function Duplicate  (      Source           : IUnknown      ;
                               &Type            : LXtPixelFormat;
                           out VObj                             ) : LxResult;
    function Load       (const FilePath         : PChar         ;
                           out VObj                             ) : LxResult;
    function Save       (      Image            : IUnknown      ;
                         const FilePath, Format : PChar         ) : LxResult;
    function Resample   (      Dest, Source     : IUnknown      ;
                               Hint             : LXtProcessHint) : LxResult;
    function Composite  (      Dest, Source     : IUnknown      ;
                         const Pos              : LXtFVector2   ) : LxResult;
    function DrawLine   (      Image            : IUnknown      ;
                         const P0, P1           : LXtFVector2   ;
                         const Color            : LXtFVector    ) : LxResult;
    function Kelvin2RGB (      Kelvin           : Single        ;
                               RGBColor         : LXtFVector    ) : LxResult;
    function RGB2Kelvin (      RGBColor         : LXtFVector    ;
                               Kelvin           : PSingle       ) : LxResult;
    function CreateCrop (      SourceImage      : IUnknown      ;
                               X, Y, W, H       : Double        ;
                           out VObj                             ) : LxResult;
  end;

  ILxImageBlockCodec = interface
  [LXu_IMAGEBLOCKCODEC]
    function  Compress  (    Image : IUnknown ;
                             Buf   : PPByte   ;
                             Size  : PInteger ) : LxResult;
    procedure Free      (    Buf   : PByte    ;
                             Size  : Integer  ) ;
    function  Decompress(    Buf   : PByte    ;
                             Size  : PInteger ;
                         out VObj             ) : LxResult;
  end;

  ILxImageFilter = interface
  [LXu_IMAGEFILTER]
    function &Type                                             : UInt32  ;
    function Generate    (      Width, Height  : Integer     ;
                                Monitor        : IUnknown    ;
                            out VObj                         ) : LxResult;
    function MultiSample (      Monitor, Image : IUnknown    ;
                            out VObj                         ) : LxResult;
    function SingleSample(const Src            : LXtColorRGBA;
                                Dest           : LXtColorRGBA) : LxResult;
  end;

  ILxImageFilterSample = interface
  [LXu_IMAGEFILTERSAMPLE]
    function SpotSample(X, Y, SpotSize : Single ;
                        RGBA           : PSingle) : LxResult;
  end;

  ILxImageFilterMetrics = interface
  [LXu_IMAGEFILTERMETRICS]
    function Generate(Metrics : PLXtImageMetrics) : LxResult;
    function Evaluate(Metrics : PLXtImageMetrics) : LxResult;
  end;

  ILxMovie = interface
  [LXu_MOVIE]
    function BeginMovie  (const FName       : PChar    ;
                                W, H, Flags : Integer  ) : LxResult;
    function SetFramerate(      FRate       : Integer  ) : LxResult;
    function AddImage    (      Image       : IUnknown ) : LxResult;
    function EndMovie                                    : LxResult;
  end;

function LXxIMD_TYPE(const T : Integer) : Integer;
function LXxIMV_TYPE(const V : Integer) : Integer;

implementation

function LXxIMD_TYPE(const T : Integer) : Integer; inline;
begin
  Result := T and LXfIMD_MASK;
end;
function LXxIMV_TYPE(const V : Integer) : Integer; inline;
begin
  Result := V and LXfIMV_MASK;
end;

end.

